﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Level26EnglishLetters4 : MonoBehaviour
{
    public List<TextButton> textButtonList = new List<TextButton>();
    public Button leftButton;
    public Button rightButton;
    public Button centerButton;
    private int activeIndex = 0;
    public GameLevelItem gameLevelItem;
    // Start is called before the first frame update
    void Start()
    {
        leftButton.onClick.AddListener(() =>
        {
            SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
            activeIndex--;
            if (activeIndex < 0)
                activeIndex = 0;
            UpdateTBBg();
        });
        rightButton.onClick.AddListener(() =>
        {
            SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
            activeIndex++;
            if (activeIndex > 3)
                activeIndex = 3;
            UpdateTBBg();
        });
        centerButton.onClick.AddListener(() =>
        {
            SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
            CenterButtonClick();
        });

        InitTextButton();
        UpdateTBBg();
    }

    private void InitTextButton()
    {
        int count = textButtonList.Count;
        for (int i = 0; i < count; i++)
        {
            textButtonList[i].Init("m", () => { });
        }
    }

    private void UpdateTBBg()
    {
        int count = textButtonList.Count;
        for (int i = 0; i < count; i++)
        {
            textButtonList[i].button.image.enabled = i <= activeIndex;
        }
    }

    private void CenterButtonClick()
    {
        if (GameCtrl.Instance.curGameState != GameState.Play)
            return;
        int count = textButtonList.Count;
        for (int i = 0; i < count; i++)
        {
            if (i <= activeIndex)
            {
                textButtonList[i].text.text = GameCtrl.Instance.GetNextLetter(textButtonList[i].text.text);
            }
        }

        JudgeResult();

    }

    private void JudgeResult()
    {
        string tempStr = string.Empty;
        int count = textButtonList.Count;
        for (int i = 0; i < count; i++)
        {
            tempStr += textButtonList[i].text.text;
        }
        if (tempStr.Equals(UConst.Str_TargetLove))
            gameLevelItem.TriggerResult(true);

    }

}
